package com.yanqu.road.dao.impl.activity.signindraw;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.signindraw.cross.CrossSignInDrawUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSignInDrawUserDataDaoImpl extends TempDataObjectDao<CrossSignInDrawUserData> {

    public static String getCrossSignInDrawUserDataSql = "select * from `t_c_sign_in_draw_user_data` where `activityId`=?";
    public Map<String, CrossSignInDrawUserData> getCrossSignInDrawUserData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, CrossSignInDrawUserData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossSignInDrawUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossSignInDrawUserData data = parse(rs);
                map.put(data.getUid(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossSignInDrawUserDataSql = "update `t_c_sign_in_draw_user_data` set rewardMap=? where activityId=? and uid=?";
    @Override
    protected boolean updateData(CrossSignInDrawUserData crossSignInDrawUserData) {
        crossSignInDrawUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSignInDrawUserData.getRewardMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSignInDrawUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSignInDrawUserData.getUid()));
        if (getDb().executeNoneQuery(updateCrossSignInDrawUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossSignInDrawUserDataSql = "insert into `t_c_sign_in_draw_user_data`(activityId,uid,rewardMap) values (?,?,?)";
    @Override
    protected boolean addData(CrossSignInDrawUserData crossSignInDrawUserData) {
        crossSignInDrawUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSignInDrawUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSignInDrawUserData.getUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSignInDrawUserData.getRewardMap())));
        if (getDb().executeNoneQuery(insertCrossSignInDrawUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossSignInDrawUserData parseData(ResultSet rs) throws SQLException {
        CrossSignInDrawUserData crossSignInDrawUserData = new CrossSignInDrawUserData();
        crossSignInDrawUserData.setActivityId(rs.getInt("activityId"));
        crossSignInDrawUserData.setUid(rs.getString("uid"));
        crossSignInDrawUserData.setRewardMap(JSON.parseObject(rs.getString("rewardMap"), new TypeReference<Map<Integer, Integer>>() {}));

        return crossSignInDrawUserData;
    }
}
